package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    //查询通用设置
    public SettingsVo settings() {
        //用户id和手机号码
        Long userId = UserHolder.getUserId();
        //1、查询用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //2、查询用户的通知开关设置
        Settings settings = settingsApi.findByUserId(userId);
        //3、获取当前用户的手机号码和ID
        String mobile = UserHolder.getMobile();
        SettingsVo vo = new SettingsVo();
        vo.setStrangerQuestion(question == null ? "你喜欢java吗" : question.getTxt());
        if(settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        vo.setPhone(mobile);
        vo.setId(userId);
        return vo;
    }

    //设置陌生人问题
    public void saveQuestions(String content) {
        //1、获取当前用户的ID
        Long userId = UserHolder.getUserId();
        //2、调用QuestionApi根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(userId);
        //3、如果Question对象存在，更新
        if(question != null) {
            question.setTxt(content);
            questionApi.update(question);
        }else {
            //4、如果不存在，保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }
    }

    //设置通知开关
    public void saveNotifications(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用API根据用户id查询通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //3、如果对象存在，更新
        if(settings != null) {
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }else {
            //4、不存在，保存
            settings = new Settings();
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setUserId(userId);
            settingsApi.save(settings);
        }
    }

    //查询黑名单
    public PageResult blacklist(Integer page, Integer pagesize) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用API分页查询数据，得到IPage对象（总数据，当前页，查询条数，数据列表）
        IPage iPage = blackListApi.findByUserId(page, pagesize, userId);
        //3、将IPage中的数据，设置到PageResult并返回
        return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }

    //移除黑名单
    public void delBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,blackUserId);
    }
}
